'use strict';
const fs = require("fs"),iconv = require('iconv-lite'), db = require("../DB"), ld = require("../loadByData");
module.exports = (request, response)=>{
    request.CODE = "GBK";
    request.EXT = ".tp";
    let DB = db.DB(request), cookie = request.PUBFUN.getCookie(request), acct_no = cookie.acct_no;
    switch(request.GET.pathname){
        case "/cgi-bin/qbas_order_query.cgi":{
            let page = 1;
            if(request.POST && request.POST.jumppage) page = request.POST.jumppage;
            if(page < 1) page = 1;
            DB.all(`select COUNT(*) rnum from 订单查询 WHERE aNo=${acct_no}`, (err, rows)=>{
                response.DATA = {"pageno":page,"pageall":1,"rows":"","optstr":""};
                if(!err) response.DATA["pageall"] = Math.ceil(rows[0].rnum/10);
                for(let p=0;p<response.DATA.pageall;p++) {
                    response.DATA.optstr += "<option value='"+(p+1)+"'>"+(p+1)+"</option>";
                }
                DB.all(`select * from 订单查询 WHERE aNo=${acct_no} LIMIT ${(page-1)*10},10`, (err, rows)=>{
                    if(!err){
                        for(let i=0;i<rows.length;i++){
                            response.DATA["rows"] += `<tr>
                            <td>${rows[i]["订单号"]}</td>
                            <td>${rows[i]["货物名称"]}</td>
                            <td class="style-grey">${rows[i]["数量"].toFixed(4)}</td>
                            <td class="style-grey">${rows[i]["支付金额"].toFixed(4)}</td>
                            <td>${rows[i]["订单状态"]}</td>
                            <td>${rows[i]["订单类型"]}</td>
                            <td>${rows[i]["下单时间"]}</td>
                            <td><a href="#" class="blue_underline"><a href=/cgi-bin/qbas_show_orderdetail.cgi?order=${rows[i]["rid"]}&status=99# target='ifrpage'>&nbsp;查看</a></td>
                          </tr>`;
                        }
                    }
                    ld(request, response, ()=>{});
                })
            });
			break;
        }case "/cgi-bin/qbas_trade_apply.cgi":{
            let page = 1;
            if(request.POST && request.POST.jumppage) page = request.POST.jumppage;
            if(page < 1) page = 1;
            DB.all(`select COUNT(*) rnum from 发货请求 WHERE aNo=${acct_no}`, (err, rows)=>{
                response.DATA = {"pageno":page,"pageall":1,"rownum":0,"rows":"","optstr":""};
                if(!err){
                    response.DATA["rownum"] = rows[0].rnum;
                    response.DATA["pageall"] = Math.ceil(rows[0].rnum/10);
                }
                DB.all(`select * from 发货请求 WHERE aNo=${acct_no} LIMIT ${(page-1)*10},10`, (err, rows)=>{
                    if(!err){
                        for(let i=0;i<rows.length;i++){
                            response.DATA["rows"] += `<tr>
                            <td><a href='/cgi-bin/qbas_trade_show_single_list.cgi?order_id=${rows[i]['订单号']}'>${rows[i]['订单号']}</a> </td>
                            <td>${rows[i]['对方申请日期']}</td>
                            <td class="style-grey"><a href='http://qb.qq.com/cgi-bin/qbas_jinhuoren_info?id=${rows[i]['对方id']}&type=1'>${rows[i]['对方帐号']}</a></td>
                            <td title="${rows[i]['对方公司名称']}" class="style-grey"><a href='http://qb.qq.com/cgi-bin/qbas_jinhuoren_info?id=${rows[i]['对方id']}&type=1'>${rows[i]['对方公司名称'].length>6?rows[i]['对方公司名称'].substr(0,6)+"...":rows[i]['对方公司名称']}</a></td>
                            <td>${rows[i]['请求进货量']}</td>
                            <td>${rows[i]['已发货量']}</td>
                            
                            
                       <!-- 	  
                            <td>        <input name="" type="text" class="inputtext" size="8" value="" maxlength="8"  />      </td>
                            
                      -->
                           
                            <td class="style-orange">${rows[i]['发货状态']}</td>
                            
                            
                            
                       <!-- 	  
                           <td nowrap="nowrap">
                            
                            <a <a href='javascript:order_money("0",126004,"shdshn2018",520833)' > ><img src="images/" alt="" width="62" height="26" /></a>
                            <a <a href='javascript:consignment("shdshn2018",520833,126004,1)' >><img src="images/" alt="" width="62" height="26" /></a>
                      
                            </td>
                       -->
                       
                            <td style="color:#ccc;"><a href='javascript:consignment("shdshn2018",520833,126004,1)' >确认发货</a> 	 </td>
                        
                        
                        
                        
                          </tr>`;
                        }
                    }
                    ld(request, response, ()=>{});
                });
            });
            break;
        }case "/cgi-bin/qbas_qrykernel_subsal.cgi":{
            let page = 1;//console.log(request.GET);
            if(request.GET.query && request.GET.query.page_no) page = request.GET.query.page_no|0;
            if(page < 1) page = 1;
            DB.all(`select COUNT(*) rnum from 下属经销商 WHERE aNo=${acct_no}`, (err, rows)=>{
                response.DATA = {"pageno":page,"pageall":1,"rownum":0,"rows":"","optstr":"","pagestr":""};
                if(!err){
                    response.DATA["rownum"] = rows[0].rnum;
                    response.DATA["pageall"] = Math.ceil(rows[0].rnum/15);
                }
                for(let p=1;p<=response.DATA.pageall;p++){
                    if(p == page) response.DATA.optstr += '<option value="'+p+'" selected >'+p+'</option>';
                    else response.DATA.optstr += '<option value="'+p+'" >'+p+'</option>';
                }
                if(page > 1) response.DATA.pagestr += `<a href="/cgi-bin/qbas_qrykernel_subsal.cgi?type=1&AcctNo=0&ProvinceID=9&ZoneVal=-1&Level=&effect=-1&ExcepAcctNo=0&public=-1&start_year=2018&start_month=04&start_day=01&end_year=2018&end_month=04&end_day=17&page_no=1"><font color="#FF0000">首页</font></a>&nbsp;<a href="/cgi-bin/qbas_qrykernel_subsal.cgi?type=1&AcctNo=0&ProvinceID=9&ZoneVal=-1&Level=&effect=-1&ExcepAcctNo=0&public=-1&start_year=2018&start_month=04&start_day=01&end_year=2018&end_month=04&end_day=17&page_no=${page-1}"><font color="#FF0000">上页</font></a>`;
                if(page < response.DATA.pageall) response.DATA.pagestr += `<a href="/cgi-bin/qbas_qrykernel_subsal.cgi?type=1&AcctNo=0&ProvinceID=9&ZoneVal=-1&Level=&effect=-1&ExcepAcctNo=0&public=-1&start_year=2018&start_month=04&start_day=01&end_year=2018&end_month=04&end_day=17&page_no=${page+1}"><font color="#FF0000">下页</font></a>&nbsp;<a href="/cgi-bin/qbas_qrykernel_subsal.cgi?type=1&AcctNo=0&ProvinceID=9&ZoneVal=-1&Level=&effect=-1&ExcepAcctNo=0&public=-1&start_year=2018&start_month=04&start_day=01&end_year=2018&end_month=04&end_day=17&page_no=${response.DATA.pageall}"><font color="#FF0000">末页</font></a>`;
                DB.all(`select * from 下属经销商 WHERE aNo=${acct_no} LIMIT ${(page-1)*15},15`, (err, rows)=>{
                    if(!err){
                        for(let i=0;i<rows.length;i++){
                            response.DATA["rows"] += `<tr>
                            <td>${i+1}</td>
                            <td scope="row">${rows[i]['所在地区']}</td>
                            <td scope="row"><a href="cgi-bin/qbas_jinhuoren_info?id=${rows[i]['id']}&type=3">${rows[i]['公司名称']}</a></td>
                            <td scope="row">${rows[i]['帐户名称']}</td>
                            <td>${rows[i]['联系人']}</td>
                            <td>${rows[i]['联系电话']}</td>
                            <td scope="row"><a href="cgi-bin/qbas_jinhuoren_info?id=${rows[i]['id']}&type=3">已绑定</a></td>
                          </tr>`;
                        }
                    }
                    ld(request, response, ()=>{});
                });
            });
            break;
        }
    }
}